/**
 * 
 */
package com.pmsco.pms.controller;

import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import com.pmsco.pms.annotation.LogonRequired;
import com.pmsco.pms.annotation.PermissionRequired;
import com.pmsco.pms.business.AdminBusiness;
import com.pmsco.pms.business.dto.AdminMainInfoRequestDTO;
import com.pmsco.pms.business.dto.AdminMainInfoReturnDTO;
import com.pmsco.pms.core.constant.PmsConstant;
import com.pmsco.pms.core.domain.PmsSysInformation;
import com.pmsco.pms.util.PmsLogonRequired;
import com.pmsco.pms.util.PmsPermissionRequired;

/**
 * @author Long Tran
 *
 */
@Controller
public class PmsAdminMainController extends AbstractPmsController {
	private static final Logger logger = LoggerFactory.getLogger(PmsAdminMainController.class);
	
	/**
	 * Save Sys Information Action
	 */
	@RequestMapping(value = PmsConstant.savePmsSysConfiguration_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_SAVE_SYS_INFO_FUNCTION)
	public View savePmsSysConfiguration(@ModelAttribute("pmsSysInformation") PmsSysInformation pmsSysInformation, Locale locale, Model model, HttpSession session) {
		logger.info("Begin Save System Information Action");
		AdminBusiness adminBusiness = new AdminBusiness();
		
		logger.debug("Build save pms configuration request dto");
		AdminMainInfoRequestDTO adminMainInfoRequestDTO = new AdminMainInfoRequestDTO();
		if(pmsSysInformation.getSysEmailAuth() == null)
			pmsSysInformation.setSysEmailAuth(PmsConstant.disableEmailAuth);
		if(pmsSysInformation.getSysEmailTlsEnable() ==  null)
			pmsSysInformation.setSysEmailTlsEnable(PmsConstant.disableEmailTls);
		adminMainInfoRequestDTO.setPmsSysInformation(pmsSysInformation);
		
		logger.debug("Call to admin business for save system information");
		AdminMainInfoReturnDTO adminMainInfoReturnDTO = adminBusiness.savePmsSysInformation(adminMainInfoRequestDTO);
		
		logger.info("End Save System Information Action");
		return new RedirectView(adminMainInfoReturnDTO.getReturnUrl());
	}
	
}
